import time
from threading import Thread


class CountdownTask:

    def __init__(self):
        self._running = True  # 定义线程状态变量

    def terminate(self):
        self._running = False

    def run(self, n):
        # run方法的主循环条件加入对状态变量的判断
        while self._running and n > 0:
            print('T-minus', n)
            n -= 1
            time.sleep(5)
        print("thread is ended")


c = CountdownTask()
th = Thread(target=c.run, args=(10,))
th.start()
# 对于耗时线程，没必要再用join()方法了，注意主线程通常也需要有个监控循环
# … any code …
# Signal termination
q = input("please press any key to quit ")
c.terminate()

